<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\index\controller;

/**
 * 商品控制器
 */
class goods extends IndexBase
{
    /**
     * 线下订单列表
     */
    public function userOrderList()
    {
        $data=$this->param;
        $where = [];
        $where['a.stat'] = 1;
        !empty($data['pay']) && $where['a.pay']=$data['pay'];
        !empty($data['search_data']) && $where['a.order_sn|b.username|b.name'] = ['like', '%'.$data['search_data'].'%'];
        $this->assign('list', $this->logicGoods->getUserOrderList($where));
        return $this->fetch('user_order_list');
    }
    /**
     * 地区
     */
    public function user_province()
    {
        $data=$this->param;
        $list=db('region')->where(['province_name'=>$data['id'],'level'=>2])->select();
        if(!empty($list)){
            $html='<option value="">请选择</option>';
            foreach($list as $k=>$v){
                $html.="<option value='".$v['name']."'>".$v['name']."</option>";
            }
            echo json_encode(array('code'=>0,'msg'=>'操作成功','html'=>$html));exit;
            //echo $html;exit;
        }else{
            echo json_encode(array('code'=>1,'msg'=>'系统数据错误'));exit;
        }
    }
    /**
     * 地区
     */
    public function user_city()
    {
        $data=$this->param;
        $list=db('region')->where(['city_name'=>$data['id'],'level'=>3])->select();
        if(!empty($list)){
            $html='<option value="">请选择</option>';
            foreach($list as $k=>$v){
                $html.="<option value='".$v['name']."'>".$v['name']."</option>";
            }
            echo $html;exit;
        }else{
            echo '系统繁忙';exit;
        }
    }
    /**
     * 商品分类添加
     */
    public function dianCatAdd()
    {
        IS_POST && $this->jump($this->logicGoods->dianCatEdit($this->param));
        $list=db('dian_cat')->select();
        $this->assign('cat_list', $list);
        return $this->fetch('dian_cat_edit');
    }

    /**
     * 分类编辑
     */
    public function dianCatEdit()
    {
        IS_POST && $this->jump($this->logicGoods->dianCatEdit($this->param));

        $info = $this->logicGoods->getdianCatInfo(['id' => $this->param['id']]);

        $this->assign('info', $info);
        $list=db('dian_cat')->select();
        $this->assign('cat_list', $list);
        return $this->fetch('dian_cat_edit');
    }

    /**
     * 门店分类列表
     */
    public function dianCatList()
    {

        $this->assign('list', $this->logicGoods->getDianCatList());

        return $this->fetch('dian_cat_list');
    }

	/**
     * 发货
     */
    public function fahuo()
    {
		if(IS_POST){
			$this->jump($this->logicGoods->fahuo($this->param));
		}else{
			$info=db('order_info')->where('id',$this->param['id'])->find();
			$finfo=db('order_goods')->where('order_sn',$info['order_sn'])->find();
			$list=db('kuaidi')->select();
			$this->assign('kdlist',$list);
			$this->assign('info',$info);
			$this->assign('finfo',$finfo);
			return $this->fetch('fahuo');
		}
		
    }
    /**
     * 确认收货
     */
    public function qrsh(){
        $this->jump($this->logicGoods->qrsh($this->param));
    }
    /**
     * 撤销订单
     */
    public function cxOrder(){
        $this->jump($this->logicGoods->cxOrder($this->param));
    }
	/**
     * 订单列表
     */
    public function orderList()
    {
        $data=$this->param;
        $where = [];
        $where['a.getid'] = session('user_id');
		$where['b.stat'] = 1;
        isset($data['stat']) && $data['stat']!=9 && $where['a.stat']=$data['stat'];
		!empty($data['pay']) && $where['b.pay']=$data['pay'];
        !empty($data['search_data']) && $where['a.order_sn|c.username|c.name'] = ['like', '%'.$data['search_data'].'%'];
        if(!empty($data['invitation'])){
            $where['a.invitation']=$data['invitation'];
            $this->assign('invitation',$data['invitation']);
        }
        $this->assign('list', $this->logicGoods->getOrderList($where));
        
        return $this->fetch('order_list');
    }
    /**
     * 订单列表
     */
    public function orderLists()
    {
        $data=$this->param;
        $where = [];
        $where['a.getid'] = session('user_id');
        $where['b.stat'] = 1;
        $where['a.stat'] = 1;
        if(!empty($data['invitation'])){
            $where['a.invitation']=$data['invitation'];
            $this->assign('invitation',$data['invitation']);
        }else{
            $where['a.id'] = 0;
        }
        $this->assign('list', $this->logicGoods->getOrderList($where));

        return $this->fetch('order_lists');
    }
    /**
     * 消费券使用
     */
    public function opOrder(){
        $param=$this->param;
        db('order_info')->startTrans();
        $info=db('order_info')->field('user_id,gid,danjia,num,order_sn,tid')->where(['id'=>$param['id']])->find();
        $ginfo=db('goods')->field('user_id,city,county,bl,btbl,lrbl,pid,invalid_time,zt_money,jt_money')->where('id',$info['gid'])->find();
        if(empty($info) || $ginfo['user_id'] != session('user_id')){
            echo json_encode(array('code'=>1,'msg'=>'系统繁忙，请稍后重试'));exit;
        }
        if(strtotime($ginfo['invalid_time']) < time()){
            echo json_encode(array('code'=>1,'msg'=>'优惠券已过期'));exit;
        }
        if($info['tid']){
            $tjid=db('user')->where('id',$info['user_id'])->value('tjid');
            if(!empty($tjid)){
                moneyyj_log($ginfo['zt_money'],$tjid,23,$info['user_id'],$info['order_sn']);
                $tjjid=db('user')->where('id',$tjid)->value('tjid');
                if(!empty($tjjid)){
                    moneyyj_log($ginfo['jt_money'],$tjjid,24,$info['user_id'],$info['order_sn']);
                }
            }
        }
        moneyyj_log($info['danjia']*$info['num']*$ginfo['bl'],$ginfo['user_id'],17,$info['user_id']);
        //fenrun($info['user_id'],$ginfo['user_id'],$info['danjia']*$info['num'],$ginfo['lrbl'],$ginfo['btbl'],$ginfo['bl'],$ginfo['city'],$ginfo['county'],$tjid,$ginfo['pid']);
        $res=db('order_info')->where(['id'=>$param['id']])->update(['stat'=>2,'sh_time'=>date('Y-m-d H:i:s')]);
        if($res != false){
            db('order_info')->commit();
            echo json_encode(array('code'=>0,'msg'=>'操作成功'));exit;
        }else{
            db('order_info')->rollback();
            echo json_encode(array('code'=>1,'msg'=>'系统繁忙，请稍后重试'));exit;
        }
    }
	/**
     * 商品分类删除
     */
    public function delDian($id = 0)
    {
        
        $this->jump($this->logicGoods->delDian($id));
    }
	/**
     * 检查用户
     */
    public function checkUsername()
    {
        $data=$this->param;		
		$name=db('user')->where('username',$data['username'])->value('name');
		if(!empty($name)){
			echo json_encode(array('code'=>0,'msg'=>'操作成功','name'=>$name));exit;
		}else{
			echo json_encode(array('code'=>1,'msg'=>'不存在该用户'));exit;
		}	
    }
	/**
     * 门店列表
     */
    public function dianList()
    {
        $data=$this->param;
        $where = [];
        isset($data['catid']) && $where['a.catid']=$data['catid'];
        !empty($data['search_data']) && $where['a.title|b.username'] = ['like', '%'.$data['search_data'].'%'];
        
        $this->assign('list', $this->logicGoods->getDianList($where, 'a.*,b.username,b.name as names', 'a.id desc'));
        
        return $this->fetch('dian_list');
    }
	/**
     * 门店添加
     */
    public function dianAdd()
    {
        $this->dianCommon();
        $plist=db('region')->where('level',1)->select();
        $this->assign('plist',$plist);
        return $this->fetch('dian_edit');
    }
	/**
     * 门店编辑
     */
    public function dianEdit()
    {
        $this->dianCommon();
		$info=db('dian')->where('id='.$this->param['id'])->find();
        !empty($info) && $info['img_ids_array'] = str2arr($info['img_ids']);
		//!empty($info) && $info['cont_ids_array'] = str2arr($info['cont_ids']);		
        $this->assign('info', $info);
        $plist=db('region')->where('level',1)->select();
        $this->assign('plist',$plist);
        if(!empty($info['province'])){
            $clist=db('region')->where(['level'=>2,'province_name'=>$info['province']])->select();
            $this->assign('clist',$clist);
        }
        if(!empty($info['city'])){
            $alist=db('region')->where(['level'=>3,'city_name'=>$info['city']])->select();
            $this->assign('alist',$alist);
        }
        return $this->fetch('dian_edit');
    }
	/**
     * 门店添加与编辑通用方法
     */
    public function dianCommon()
    {      
        IS_POST && $this->jump($this->logicGoods->dianEdit($this->param));
        $list=db('dian_cat')->select();
        $this->assign('cat_list', $list);
    }
    /**
     * 商品列表
     */
    public function goodsList()
    {
        $where = ['user_id'=>session('user_id')];
        $this->assign('list', $this->logicGoods->getGoodsList($where, 'a.*,b.title as titles,c.username,c.name', 'a.id desc'));
        return $this->fetch('goods_list');
    }
    /**
     * 商品添加
     */
    public function goodsAdd()
    {
        $this->goodsCommon();
        $plist=db('region')->where('level',1)->select();
        $this->assign('plist',$plist);
        return $this->fetch('goods_edit');
    }
    
    /**
     * 商品编辑
     */
    public function goodsEdit()
    {
        
        $this->goodsCommon();
        
		$info=db('goods')->where('id='.$this->param['id'])->find();
   
        !empty($info) && $info['img_ids_array'] = str2arr($info['img_ids']);
		!empty($info) && $info['cont_ids_array'] = str2arr($info['cont_ids']);
        $info['username']=db('user')->where('id',$info['user_id'])->value('username');
        $info['name']=db('user')->where('id',$info['user_id'])->value('name');
        $this->assign('info', $info);
        $plist=db('region')->where('level',1)->select();
        $this->assign('plist',$plist);
        if(!empty($info['province'])){
            $clist=db('region')->where(['level'=>2,'province_name'=>$info['province']])->select();
            $this->assign('clist',$clist);
        }
        if(!empty($info['city'])){
            $alist=db('region')->where(['level'=>3,'city_name'=>$info['city']])->select();
            $this->assign('alist',$alist);
        }
        return $this->fetch('goods_edit');
    }
    
    /**
     * 商品添加与编辑通用方法
     */
    public function goodsCommon()
    {
        
        IS_POST && $this->jump($this->logicGoods->goodsEdit($this->param));
        $list=db('goods_cat')->where('pid',0)->select();
        foreach($list as $k=>$v){
            $catlist=db('goods_cat')->field('id,title')->where('pid',$v['id'])->select();
            foreach($catlist as $key=>$val){
                $catlist[$key]['catlist']=db('goods_cat')->field('id,title')->where('pid',$val['id'])->select();
            }
            $list[$k]['catlist']=$catlist;
        }
        $this->assign('cat_list', $list);
    }
    
    /**
     * 商品分类添加
     */
    public function goodsCatAdd()
    {
        
        IS_POST && $this->jump($this->logicGoods->goodsCatEdit($this->param));
        $list=db('goods_cat')->where('pid',0)->select();
        $this->assign('cat_list', $list);
        return $this->fetch('goods_cat_edit');
    }
    
    /**
     * 商品分类编辑
     */
    public function goodsCatEdit()
    {
        
        IS_POST && $this->jump($this->logicGoods->goodsCatEdit($this->param));
        
        $info = $this->logicGoods->getgoodsCatInfo(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        $list=db('goods_cat')->where('pid',0)->select();
        $this->assign('cat_list', $list);
        return $this->fetch('goods_cat_edit');
    }
    
    /**
     * 商品分类列表
     */
    public function goodsCatList()
    {
        $list=$this->logicGoods->getGoodsCatList();
        foreach($list as $k=>$v){
            $list[$k]['catlist']=db('goods_cat')->where('pid',$v['id'])->select();
        }
        $this->assign('list', $list);
        return $this->fetch('goods_cat_list');
    }
    
	/**
     * 商品分类删除
     */
    public function delGoods($id = 0)
    {
        
        $this->jump($this->logicGoods->delGoods($id));
    }
    /**
     * 商品分类删除
     */
    public function goodsCatDel($id = 0)
    {
        
        $this->jump($this->logicGoods->goodsCatDel($id));
    }
    
    /**
     * 数据状态设置
     */
    public function setStatus()
    {
        
        $this->jump($this->logicAdminBase->setStatus('goods', $this->param));
    }
    /**
     * 数据状态设置
     */
    public function setDianStatus()
    {
        $data=$this->param;
        if($data['status'] == 0){
            $user_id=db('dian')->where('id',$data['ids'])->value('user_id');
            db('user')->where('id',$user_id)->update(['is'=>0]);
        }else{
            $user_id=db('dian')->where('id',$data['ids'])->value('user_id');
            db('user')->where('id',$user_id)->update(['is'=>1]);
        }
        $this->jump($this->logicAdminBase->setStatus('dian', $this->param));
    }
    /**
     * 排序
     */
    public function setSort()
    {

        $this->jump($this->logicAdminBase->setSort('GoodsCat', $this->param));
    }
}
